#!/usr/bin/env python
# encoding: utf-8
# @author: Lin Han
# @contact: voldemort22@126.com
# @file: receiver.py
# @time: 2021/3/19 16:06
# @desc:
import json

import pika
import requests

from application.message_queue.process import history_clamp_process
from views.configs import CONSUME_QUEUE, MQ_ACCOUNT, MQ_PASSWORD, MQ_PORT, MQ_IP, DEFAULT_QUEUE

credentials = pika.PlainCredentials(MQ_ACCOUNT, MQ_PASSWORD)
parameters = pika.ConnectionParameters(MQ_IP, MQ_PORT, "/", credentials)
connection = pika.BlockingConnection(parameters)
channel = connection.channel()

channel.queue_declare(queue=DEFAULT_QUEUE, durable=True)


def callback(channel, method, properties, number):
    # requests.request(
    #     method="put",
    #     # url=f"http://localhost:{BACKEND_PORT}/message_queue/v1/update_data",
    #     url=f"http://{BACKEND_URL}:{BACKEND_PORT}/message_queue/v1/update_data",
    #     # data=json.dumps(content),
    #     data=str(body, encoding="utf-8").encode('utf-8'),
    # )
    if number[5] == "1":
        history_clamp_process(number)
    # elif number[5] == "2":
    #     history_part_process(number)
    # elif number[5] == "3":
    #     standard_clamp_process(number)
    # elif number[5] == "4":
    #     standard_part_process(number)
    # elif number[5] == "5":
    #     parameter_model_process(number)
#


if __name__ == '__main__':
    channel.basic_consume(
        on_message_callback=callback,
        queue=DEFAULT_QUEUE,
        auto_ack=True,
    )

    print('[*] Waiting for messages. To exit press CTRL+C')
    channel.start_consuming()
